using ExamServer.Entities;
using Microsoft.EntityFrameworkCore;
using System;

namespace ExamServer.Service
{
  public class ExamServerContext : DbContext
  {
    public ExamServerContext(DbContextOptions<ExamServerContext> options) : base(options)
    {

    }

    public DbSet<QuestionType> QuestionTypes { get; set; }
    public DbSet<Users> Uesrs { get; set; }
    public DbSet<Menus> Menus { get; set; }
    public DbSet<tbRoles> Roles{get;set;}
    
    public DbSet<tbUsersAndRoles> UsersAndRoles{get;set;}
    public DbSet<tbRolesAndMenus> RoleAndMenus{get;set;}
    public DbSet<viewUserHaveMenus> viewUserHaveMenus{get;set;}//视图

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
      base.OnModelCreating(modelBuilder);
      //试题类型表设定
      modelBuilder.Entity<QuestionType>().HasData(
          new QuestionType
          {
            Id = 1,
            TypeName = "选择题",
            TypeState = StateEnum.可用,
            Remark = "选择题备注"

          },
          new QuestionType
          {
            Id = 2,
            TypeName = "填空题",
            TypeState = StateEnum.不可用,
            Remark = "填空题备注"
          }
          );
      //用户表的设定
      modelBuilder.Entity<Users>().ToTable("tbUsers").HasData(
          new Users
          {
            Id = Guid.NewGuid(),
            Name = "张三",
            Password = "123456",
            Birthday = "1990-01-01",
            Email = "zhangsan@163.com"
          },
          new Users
          {
            Id = Guid.NewGuid(),
            Name = "李四",
            Password = "123456",
            Birthday = "1992-01-01",
            Email = "lisi@qq.com"
          }
          );
      //菜单表的设定
      modelBuilder.Entity<Menus>().ToTable("tbMenus").HasKey(x => x.Id);
      //角色表的设定
      modelBuilder.Entity<tbRoles>().ToTable("tbRoles");
      // .HasData(
      //   new tbRoles{
      //     ID=0,
      //     RoleName="超级管理员",
      //     RoleState=1
      //   },
      //   new tbRoles{
      //     ID=0,
      //     RoleName="普通用户",
      //     RoleState=1
      //   }
      // );
    //modelBuilder.Entity<tbRoles>().HasKey(x=>x.ID); 可写可不写
      //用户与角色关联表配置
      modelBuilder.Entity<tbUsersAndRoles>().HasKey(uk=>new{uk.UID,uk.RID});
      modelBuilder.Entity<tbUsersAndRoles>().HasOne<Users>(ur=>ur.User)
          .WithMany(u=>u.UserRoles)
          .HasForeignKey(u=>u.UID);
      modelBuilder.Entity<tbUsersAndRoles>().HasOne<tbRoles>(ur=>ur.Role)
          .WithMany(r=>r.UserRoles)
          .HasForeignKey(r=>r.RID);

      //角色与菜单关联表配置
      modelBuilder.Entity<tbRolesAndMenus>().ToTable("tbRolesAndMenus").HasKey(x=>x.ID);
      //配置关联表与角色外键
      modelBuilder.Entity<tbRolesAndMenus>().HasOne<tbRoles>(rm=>rm.Role)
      .WithMany(r=>r.RolesAndMenus)
      .HasForeignKey(r=>r.Rid);
      //配置关联表与菜单外键
      modelBuilder.Entity<tbRolesAndMenus>().HasOne<Menus>(rm=>rm.Menu)
      .WithMany(m=>m.RoleAndMenus)
      .HasForeignKey(m=>m.MenuID);    
      //视图的配置
      modelBuilder.Entity<viewUserHaveMenus>().HasNoKey().ToView("viewUserHaveMenus");
    }
  }
}